// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener('DOMContentLoaded', () => {
  setTimeout(() => {
    document.body.classList.add('sidenav-pinned')
    document.body.classList.add('ready')
  }, 200)
})

function tip(e){
  const myToast = document.querySelector('#myToast')
  const toast = new bootstrap.Toast(myToast,{delay:2000})
  myToast.querySelector('.toast-body').innerHTML = e
  toast.show()
}


axios.defaults.baseURL = 'http://ajax-api.itheima.net'


 const navbar = document.querySelector('.navbar-nav .font-weight-bold')

 if(navbar){
   navbar.innerHTML = localStorage.getItem('user-name')
 }


 const logout = document.querySelector('#logout')

 if(logout){
  logout.addEventListener('click',function(){
     localStorage.removeItem('user-token')
     localStorage.removeItem('user-name')
     location.href = 'login.html'
  })
 }



 // *******************请求拦截器
axios.interceptors.request.use(function (config) {
  const token = localStorage.getItem('user-token')
  // console.log(config);
  if(token){
    config.headers.authorization = token
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});


// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  
  return response.data;
}, function (error) {
  // 对响应错误做点什么
  console.dir(error);
  if(error.response.status === 401){
    alert('登录失效,请重新登录')
    return location.href = 'login.html'
  }
  return Promise.reject(error);
});
